package com.huikeportal.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.huikeportal.controller.common.Lang;
import com.huikeportal.model.Language;
import com.huikeportal.model.MenuInfo;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.RoleInfo;
import com.huikeportal.model.RolePageButton;
import com.huikeportal.model.TreeHtml;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.WordBook;
import com.huikeportal.model.common.SqlParameter;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.RoleService;
import com.huikeportal.service.SelectSiteService;

/**
 * 角色管理
 * 
 * @author XIAKUN
 * 
 */
@Controller
@Scope("prototype")
public class RoleController {

	@Autowired
	private HttpSession session;

	@Autowired
	private LoginService loginService;

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private SelectSiteService selectSiteService;

	@InitBinder("roleInfo")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("roleInfo.");
	}

	/**
	 * 初始化加载以及点击树节点查询
	 * 
	 * @param roleInfo
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/roleManager")
	public String roleManager(RoleInfo roleInfo, ModelMap model)
			throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		userInfo = loginService.getUserInfo(userInfo);
		// 获取当前系统语言
		String sysLang = session.getAttribute("sysLang").toString();
		// 获取用户语言包
		Language language = Lang.getInfo(sysLang);
		// 查询当前页面id
		SqlParameter sql = new SqlParameter();
		sql.setSysLang(sysLang);
		sql.setString("userManager");
		Integer menuId = selectSiteService.getMenuId(sql);
		// 获取用户权限按钮信息
		List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
				userInfo.getUserId(), menuId);
		// 按钮过滤
		language = filterLanguage(language, buttonList);
		model.addAttribute("language", language);
		String menuHtml = loginService.getMenuHtml(userInfo, "roleManager");
		// 初始化显示自己的角色
		if ("".equals(roleInfo.getRoleId()) || roleInfo.getRoleId() == null) {
			roleInfo.setRoleId(userInfo.getRoleId());
		}
		String roleTreeHtml = roleService.getRoleTreeHtml(userInfo, roleInfo
				.getRoleId());
		List<RolePageButton> roleHtml = roleService.getButtonHtml(userInfo,
				roleInfo.getRoleId());
		roleInfo.setUserRoleId(userInfo.getRoleId());
		// 将所有一级页面的id拼接成字符串传回页面
		String divString = "";
		// 获取所有一级页面
		List<MenuInfo> oneList = roleService.getPageInfo("1", roleHtml,
				userInfo.getSysLang());
		for (int i = 1; i < oneList.size() + 1; i++) {
			oneList.get(i - 1).setSortId(i);
			divString += oneList.get(i - 1).getMenuId() + ",";
		}
		divString = divString.substring(0, divString.length() - 1);
		// 获取所有二级页面
		List<MenuInfo> twoList = roleService.getPageInfo("2", roleHtml,
				userInfo.getSysLang());
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("roleTreeHtml", roleTreeHtml);// 角色树html
		model.addAttribute("oneList", oneList);// 一级菜单
		model.addAttribute("twoList", twoList);// 二级菜单
		model.addAttribute("roleHtml", roleHtml);// 页面权限按钮
		model.addAttribute("roleInfo", roleInfo);// 角色信息
		model.addAttribute("divString", divString);// 一级菜单id字符串
		return "roleManager";
	}

	/**
	 * 按钮过滤
	 * 
	 * @param language
	 *            语言包
	 * @param buttonList
	 *            按钮权限
	 * @return
	 * @throws Exception
	 */
	private Language filterLanguage(Language language, List<WordBook> buttonList)
			throws Exception {
		boolean add = false;
		boolean modify = false;
		boolean delete = false;
		for (int i = 0; i < buttonList.size(); i++) {
			if ("2".equals(buttonList.get(i).getDicCode())) {
				add = true;
			}
			if ("3".equals(buttonList.get(i).getDicCode())) {
				modify = true;
			}
			if ("4".equals(buttonList.get(i).getDicCode())) {
				delete = true;
			}
		}
		if (!add) {
			language.setAdd("");
		}
		if (!modify) {
			language.setModify("");
		}
		if (!delete) {
			language.setDelete("");
		}
		return language;
	}
	
	/**
	 * 新增角色初始化
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/roleManagerAdd")
	public String roleManagerAdd(ModelMap model) throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		String menuHtml = loginService.getMenuHtml(userInfo, "roleManager");
		List<TreeHtml> roleList = roleService.getRoleList(userInfo);
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("roleList", roleList);// 父角色下拉框数据
		return "roleManagerAdd";
	}

	/**
	 * 新增角色提交
	 * 
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/roleAddSub")
	public String roleAddSub(RoleInfo roleInfo, ModelMap model)
			throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		// 获取用户信息
		userInfo = loginService.getUserInfo(userInfo);
		// 获取当前系统语言
		String sysLang = session.getAttribute("sysLang").toString();
		// 获取用户语言包
		Language language = Lang.getInfo(sysLang);
		// 查询当前页面id
		SqlParameter sql = new SqlParameter();
		sql.setSysLang(sysLang);
		sql.setString("userManager");
		Integer menuId = selectSiteService.getMenuId(sql);
		// 获取用户权限按钮信息
		List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
				userInfo.getUserId(), menuId);
		// 按钮过滤
		language = filterLanguage(language, buttonList);
		model.addAttribute("language", language);
		String message = roleService.addRole(roleInfo, userInfo);
		String menuHtml = loginService.getMenuHtml(userInfo, "roleManager");
		String roleTreeHtml = "";
		List<RolePageButton> roleHtml = new ArrayList<RolePageButton>();
		roleTreeHtml = roleService.getRoleTreeHtml(userInfo, userInfo
				.getRoleId());
		roleHtml = roleService.getButtonHtml(userInfo, userInfo.getRoleId());
		// 获取所有一级页面
		List<MenuInfo> oneList = roleService.getPageInfo("1", roleHtml,
				userInfo.getSysLang());
		for (int i = 1; i < oneList.size() + 1; i++) {
			oneList.get(i - 1).setSortId(i);
		}
		// 获取所有二级页面
		List<MenuInfo> twoList = roleService.getPageInfo("2", roleHtml,
				userInfo.getSysLang());
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("roleTreeHtml", roleTreeHtml);// 角色树html
		model.addAttribute("oneList", oneList);// 一级菜单
		model.addAttribute("twoList", twoList);// 二级菜单
		model.addAttribute("roleHtml", roleHtml);// 页面权限按钮
		model.addAttribute("message", message);
		return "roleManager";
	}

	/**
	 * 角色编辑初始化
	 * 
	 * @param roleInfo
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/roleManagerEdit")
	public String roleManagerEdit(RoleInfo roleInfo, ModelMap model)
			throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		roleInfo = roleService.getRoleByRoleId(roleInfo);
		List<TreeHtml> roleList = roleService.getRoleList(userInfo);
		String menuHtml = loginService.getMenuHtml(userInfo, "roleManager");
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("roleList", roleList);// 父角色下拉框数据
		model.addAttribute("roleInfo", roleInfo);// 角色信息
		return "roleManagerEdit";
	}

	/**
	 * 角色编辑提交
	 * 
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/roleEditSub")
	public String roleEditSub(RoleInfo roleInfo, ModelMap model)
			throws Exception {
		String message = roleService.editRole(roleInfo);
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		// 获取用户信息
		userInfo = loginService.getUserInfo(userInfo);
		// 获取当前系统语言
		String sysLang = session.getAttribute("sysLang").toString();
		// 获取用户语言包
		Language language = Lang.getInfo(sysLang);
		// 查询当前页面id
		SqlParameter sql = new SqlParameter();
		sql.setSysLang(sysLang);
		sql.setString("userManager");
		Integer menuId = selectSiteService.getMenuId(sql);
		// 获取用户权限按钮信息
		List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
				userInfo.getUserId(), menuId);
		// 按钮过滤
		language = filterLanguage(language, buttonList);
		model.addAttribute("language", language);
		String menuHtml = loginService.getMenuHtml(userInfo, "roleManager");
		String roleTreeHtml = "";
		List<RolePageButton> roleHtml = new ArrayList<RolePageButton>();
		roleTreeHtml = roleService.getRoleTreeHtml(userInfo, userInfo
				.getRoleId());
		roleHtml = roleService.getButtonHtml(userInfo, userInfo.getRoleId());
		// 获取所有一级页面
		List<MenuInfo> oneList = roleService.getPageInfo("1", roleHtml,
				userInfo.getSysLang());
		for (int i = 1; i < oneList.size() + 1; i++) {
			oneList.get(i - 1).setSortId(i);
		}
		// 获取所有二级页面
		List<MenuInfo> twoList = roleService.getPageInfo("2", roleHtml,
				userInfo.getSysLang());
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("roleTreeHtml", roleTreeHtml);// 角色树html
		model.addAttribute("oneList", oneList);// 一级菜单
		model.addAttribute("twoList", twoList);// 二级菜单
		model.addAttribute("roleHtml", roleHtml);// 页面权限按钮
		model.addAttribute("message", message);
		return "roleManager";
	}

	/**
	 * 保存配置
	 * 
	 * @param roleInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveConfig")
	public String saveConfig(RoleInfo roleInfo, ModelMap model)
			throws Exception {
		String message = roleService.saveConfig(roleInfo);
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		// 获取用户信息
		userInfo = loginService.getUserInfo(userInfo);
		// 获取当前系统语言
		String sysLang = session.getAttribute("sysLang").toString();
		// 获取用户语言包
		Language language = Lang.getInfo(sysLang);
		// 查询当前页面id
		SqlParameter sql = new SqlParameter();
		sql.setSysLang(sysLang);
		sql.setString("userManager");
		Integer menuId = selectSiteService.getMenuId(sql);
		// 获取用户权限按钮信息
		List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
				userInfo.getUserId(), menuId);
		// 按钮过滤
		language = filterLanguage(language, buttonList);
		model.addAttribute("language", language);
		String menuHtml = loginService.getMenuHtml(userInfo, "roleManager");
		String roleTreeHtml = "";
		List<RolePageButton> roleHtml = new ArrayList<RolePageButton>();
		roleTreeHtml = roleService.getRoleTreeHtml(userInfo, userInfo
				.getRoleId());
		roleHtml = roleService.getButtonHtml(userInfo, userInfo.getRoleId());
		// 获取所有一级页面
		List<MenuInfo> oneList = roleService.getPageInfo("1", roleHtml,
				userInfo.getSysLang());
		for (int i = 1; i < oneList.size() + 1; i++) {
			oneList.get(i - 1).setSortId(i);
		}
		// 获取所有二级页面
		List<MenuInfo> twoList = roleService.getPageInfo("2", roleHtml,
				userInfo.getSysLang());
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("roleTreeHtml", roleTreeHtml);// 角色树html
		model.addAttribute("oneList", oneList);// 一级菜单
		model.addAttribute("twoList", twoList);// 二级菜单
		model.addAttribute("roleHtml", roleHtml);// 页面权限按钮
		model.addAttribute("message", message);
		return "roleManager";
	}

	/**
	 * 删除角色
	 * 
	 * @param roleInfo
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteRole")
	public String deleteRole(RoleInfo roleInfo, ModelMap model)
			throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		// 获取用户信息
		userInfo = loginService.getUserInfo(userInfo);
		// 获取当前系统语言
		String sysLang = session.getAttribute("sysLang").toString();
		// 获取用户语言包
		Language language = Lang.getInfo(sysLang);
		// 查询当前页面id
		SqlParameter sql = new SqlParameter();
		sql.setSysLang(sysLang);
		sql.setString("userManager");
		Integer menuId = selectSiteService.getMenuId(sql);
		// 获取用户权限按钮信息
		List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
				userInfo.getUserId(), menuId);
		// 按钮过滤
		language = filterLanguage(language, buttonList);
		model.addAttribute("language", language);
		String menuHtml = loginService.getMenuHtml(userInfo, "roleManager");
		MessageCommon messageCommon = roleService.deleteRole(roleInfo);
		String roleTreeHtml = "";
		List<RolePageButton> roleHtml = new ArrayList<RolePageButton>();
		// 如果删除成功
		if (messageCommon.isFlag()) {
			roleTreeHtml = roleService.getRoleTreeHtml(userInfo, userInfo
					.getRoleId());
			roleHtml = roleService
					.getButtonHtml(userInfo, userInfo.getRoleId());
		}
		// 如果删除失败，还选中要删除的角色
		else {
			roleTreeHtml = roleService.getRoleTreeHtml(userInfo, roleInfo
					.getRoleId());
			roleHtml = roleService
					.getButtonHtml(userInfo, roleInfo.getRoleId());
		}
		// 获取所有一级页面
		List<MenuInfo> oneList = roleService.getPageInfo("1", roleHtml,
				userInfo.getSysLang());
		for (int i = 1; i < oneList.size() + 1; i++) {
			oneList.get(i - 1).setSortId(i);
		}
		// 获取所有二级页面
		List<MenuInfo> twoList = roleService.getPageInfo("2", roleHtml,
				userInfo.getSysLang());
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		model.addAttribute("roleTreeHtml", roleTreeHtml);// 角色树html
		model.addAttribute("oneList", oneList);// 一级菜单
		model.addAttribute("twoList", twoList);// 二级菜单
		model.addAttribute("roleHtml", roleHtml);// 页面权限按钮
		model.addAttribute("message", messageCommon.getMessage());
		return "roleManager";
	}
}
